<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Apotik extends CI_Controller {

	function __construct() {
		parent::__construct();

		//jika digunakan di mayoritas function, panggil disini
		//model
		$this->load->model('m_apotik');

		//library breadcrum/untuk navigasi
		$this->load->library(array('breadcrumb','form_validation'));
		$this->load->helper(array('slug','terbilang'));
	}

	function index() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Data Klaim Obat ABHP');

		//meta description jika perlu
		$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

		//css tambahan bila perlu
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');
		$this->layout->set_include('js', 'themes/back/bundle/select2/select2.min.js');
		$this->layout->set_include('css', 'themes/back/bundle/select2/select2.min.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Klaim Obat ABHP');		

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load library "paging"
	    $config['sql']      				= $this->m_apotik->get();//Query SQL - bukan array lho ya...
	    $config['per_page'] 				= 15;                            //jumlah item tiap halaman
	    $data = $this->paging->create_pagination($config);   				//Load fungsi di library "paging" untuk membuat pagination. 

    //judul besar
		$data['primary_title'] 			= '<i class="ion-android-note"></i> Apotik';
		$data['sub_primary_title'] 	= 'Manajemen data klaim obat & ABHP';
		$data['ppk']=$this->m_apotik->getPPK();

		//menghilangka session jika ada
		$this->session->unset_userdata('keyword');

		//menggunakan theme back
		$this->layout->theme('back','apotik/index', $data);
	}

	function cari() {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//Layout
		//title
		$this->layout->set_title('Cari Data Klaim Obat ABHP');

		//meta description jika perlu
		$this->layout->set_desc('Made with love by Raksa Abadi Informatika');

		//css tambahan bila perlu
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');
		$this->layout->set_include('js', 'themes/back/bundle/select2/select2.min.js');
		$this->layout->set_include('css', 'themes/back/bundle/select2/select2.min.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Klaim Obat ABHP', site_url('apotik'));
		$this->breadcrumb->add_crumb('Cari Klaim Obat ABHP');
		//buat cari
		if($_POST['nama']!=''){
			$data['keyword'] = $this->input->post('nama');
			$this->session->set_userdata('keyword', $data['keyword']);
			$config['sql']   = $this->m_apotik->get_cari_nama($data['keyword']);
		} elseif($_POST['ppk']!=''&&$_POST['ppk']!="all") {
			if ($_POST['tgl_transaksi']!='') {
				$data['keyword'] = $this->input->post('ppk');
				$data['tgl'] = iddate2dbdate($this->input->post('tgl_transaksi'));
				$this->session->set_userdata('keyword', $data['keyword']);
				$config['sql']   = $this->m_apotik->get_cari_tgl($data['keyword'],$data['tgl']);
			}else{
				$data['keyword'] = $this->input->post('ppk');
				$this->session->set_userdata('keyword', $data['keyword']);
				$config['sql']   = $this->m_apotik->get_cari_ppk($data['keyword']);
			}
		}elseif($_POST['ppk']!=''&&$_POST['ppk']=="all"){
			if ($_POST['tgl_transaksi']!='') {
				$data['keyword'] = $this->input->post('ppk');
				$data['tgl'] = iddate2dbdate($this->input->post('tgl_transaksi'));
				$this->session->set_userdata('keyword', $data['keyword']);
				$config['sql']   = $this->m_apotik->get_cari_tgl_all($data['tgl']);
			}else{
				$data['keyword'] = $this->input->post('ppk');
				$this->session->set_userdata('keyword', $data['keyword']);
				$config['sql']   = $this->m_apotik->get_cari_ppk_all();
			}
		}	

		//paging dan ambil data dari model
		$this->load->library('paging');                         		//Load the "paging" library
	   //your SQL - bukan array lho ya...
	    $config['per_page'] 				= 15;                            //Display items per page
	    $data = $this->paging->create_pagination($config);   				//Load function in "paging" libraryfor create pagination. 

    //judul besar
		$data['primary_title'] 			= '<i class="ion-android-note"></i> Apotik';
		$data['sub_primary_title'] 	= 'Manajemen data klaim obat & ABHP';
		$data['ppk']=$this->m_apotik->getPPK();
		//menggunakan theme back
		$this->layout->theme('back','apotik/index', $data);
	}

	function detail($id_klaim_obat = '') {

		//rule type pada function ini: Read
		$this->rule->type('R');

		//parameter yang dipelukan query
		$params = array($id_klaim_obat);

		//Layout
		//title
		$this->layout->set_title('Detail Klaim Obat ABHP');

		//meta description jika perlu
		//$this->layout->set_desc($this->m_apotik->get_ket($params));

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Klaim Obat ABHP', site_url('apotik'));
		$this->breadcrumb->add_crumb('Detail Klaim Obat ABHP');

		//judul besar
		$data['primary_title'] = '<i class="ion-android-note"></i> Apotik';
		$data['sub_primary_title'] = 'Detail klaim obat & ABHP';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] = $this->m_apotik->get_apotik_by($params);
		$data['klaim_obat'] 	= $this->m_apotik->get_export_obat($keyword=$id_klaim_obat);
		$data['jumlah'] 	= count($data['klaim_obat']);
		$portir = $this->m_apotik->get_export_jasa($params=array($keyword=$id_klaim_obat,$kode='P'));
			if($portir==NULL){
				$data['tarif_portir']="-";
				$data['portir_tarif']=0;
				$data['jml_portir']=0;
			}else{
				$data['tarif_portir']=formatRupiahTabel($portir['tarif_diminta']*$portir['frekuensi_diminta']);
				$data['portir_tarif']=$portir['tarif_diminta'];
				$data['jml_portir']=$portir['frekuensi_diminta'];
			}
		$resep = $this->m_apotik->get_export_jasa($params=array($keyword=$id_klaim_obat,$kode='R'));
			if($resep==NULL){
				$data['tarif_resep']="-";
				$data['resep_tarif']=0;
				$data['jml_resep']=0;
			}else{
				$data['tarif_resep']=formatRupiahTabel($resep['tarif_diminta']*$resep['frekuensi_diminta']);
				$data['resep_tarif']=$resep['tarif_diminta'];
				$data['jml_resep']=$resep['frekuensi_diminta'];
			}
		$oksigen = $this->m_apotik->get_export_jasa($params=array($keyword=$id_klaim_obat,$kode='O2'));
			if($oksigen==NULL){
				$data['tarif_oksigen']="-";
				$data['oksigen_tarif']=0;
				$data['jml_oksigen']=0;
			}else{
				$data['tarif_oksigen']=formatRupiahTabel($oksigen['tarif_diminta']*$oksigen['frekuensi_diminta']);
				$data['oksigen_tarif']=$oksigen['tarif_diminta'];
				$data['jml_oksigen']=$oksigen['frekuensi_diminta'];
			}
			

		//menggunakan theme back
		$this->layout->theme('back','apotik/detail', $data);
	}

	function cetak($id_klaim_obat = '') {

		//rule type pada function ini: Read
		$this->rule->type('R');
		$this->load->helper('umur');

		//parameter yang dipelukan query
		$params = array($id_klaim_obat);

		//Layout
		//title
		$this->layout->set_title('Detail Klaim Obat ABHP');

		//meta description jika perlu
		//$this->layout->set_desc($this->m_apotik->get_ket($params));

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Klaim Obat ABHP', site_url('apotik'));
		$this->breadcrumb->add_crumb('Detail Klaim Obat ABHP');

		//judul besar
		$data['primary_title'] = '<i class="ion-android-note"></i> Apotik';
		$data['sub_primary_title'] = 'Detail klaim obat & ABHP';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] = $this->m_apotik->get_apotik_by($params);
		$data['klaim_obat'] 	= $this->m_apotik->get_export_obat($keyword=$id_klaim_obat);
		$data['jumlah'] 	= count($data['klaim_obat']);
		$data['jam'] = date('h:i:s A');
		$portir = $this->m_apotik->get_export_jasa($params=array($keyword=$id_klaim_obat,$kode='P'));
			if($portir==NULL){
				$data['tarif_portir']="-";
				$data['portir_tarif']=0;
				$data['jml_portir']=0;
			}else{
				$data['tarif_portir']=formatRupiahTabelB($portir['tarif_diminta']*$portir['frekuensi_diminta']);
				$data['portir_tarif']=$portir['tarif_diminta'];
				$data['jml_portir']=$portir['frekuensi_diminta'];
			}
		$resep = $this->m_apotik->get_export_jasa($params=array($keyword=$id_klaim_obat,$kode='R'));
			if($resep==NULL){
				$data['tarif_resep']="-";
				$data['resep_tarif']=0;
				$data['jml_resep']=0;
			}else{
				$data['tarif_resep']=formatRupiahTabelB($resep['tarif_diminta']*$resep['frekuensi_diminta']);
				$data['resep_tarif']=$resep['tarif_diminta'];
				$data['jml_resep']=$resep['frekuensi_diminta'];
			}
		$oksigen = $this->m_apotik->get_export_jasa($params=array($keyword=$id_klaim_obat,$kode='O2'));
			if($oksigen==NULL){
				$data['tarif_oksigen']="-";
				$data['oksigen_tarif']=0;
				$data['jml_oksigen']=0;
			}else{
				$data['tarif_oksigen']=formatRupiahTabelB($oksigen['tarif_diminta']*$oksigen['frekuensi_diminta']);
				$data['oksigen_tarif']=$oksigen['tarif_diminta'];
				$data['jml_oksigen']=$oksigen['frekuensi_diminta'];
			}
			

		//menggunakan theme back
		$this->load->view('back/layouts/apotik/print', $data);
	}

	function add() {

		//rule type pada function ini: Create
		$this->rule->type('C');

		//Layout
		//title
		$this->layout->set_title('Tambah Data Klaim Obat ABHP');

		//js tambahan
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');
		$this->layout->set_include('js', 'themes/back/bundle/select2/select2.min.js');
		$this->layout->set_include('css', 'themes/back/bundle/select2/select2.min.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Klaim Obat ABHP', site_url('apotik'));
		$this->breadcrumb->add_crumb('Tambah Klaim Obat ABHP');

		//judul besar
		$data['primary_title'] = 'Apotik';
		$data['sub_primary_title'] = 'Input data klaim obat & ABHP';
		$data['tgl_transaksi']=date('d/m/Y');


		//menggunakan theme back
		$this->layout->theme('back','apotik/add', $data);
	}

	function save() {
		//rule type pada function ini: Create
		$this->rule->type('C');
		$user = $this->ion_auth->user()->row();
			$dataInsert = array(
				'kd_klaim' => $this->input->post('kd_klaim'),
				'kd_dokter' => $this->input->post('dokter'),
				'kd_unit' => $this->input->post('unit'),
				'dmk' => $this->input->post('dmk'),
				'tgl_transaksi' => iddate2dbdate($this->input->post('tgl_transaksi')),
				'id_petugas' => $user->id
			);

			//simpan melalui model
			//$this->m_apotik->save($dataInsert);

			if(!$this->m_apotik->save($dataInsert)){
				$id_klaim_obat=$this->db->insert_id();
				//$this->session->set_flashdata('gagal', 'Kode klaim <b>'.$this->input->post('kd_klaim').'</b>, dari peserta <b>'.$this->input->post('nama').'</b> gagal ditambahkan.');
				redirect('apotik/edit/'.$id_klaim_obat,'refresh');
			}else{
				$id_klaim_obat=$this->db->insert_id();
				//$this->session->set_flashdata('sukses', 'Kode klaim <b>'.$this->input->post('kd_klaim').'</b> dari peserta <b>'.$this->input->post('nama').'</b> berhasil ditambahkan.');
				redirect('apotik/edit/'.$id_klaim_obat,'refresh');
			}

			redirect('apotik/edit/'.$id_klaim_obat);
		
	}

	function edit($id_klaim_obat = '') {

		//rule type pada function ini: Update
		$this->rule->type('U');

		//parameter yang dipelukan query
		$params = array($id_klaim_obat);

		//layout
		//title
		$this->layout->set_title('Edit Data Klaim Obat ABHP');

		//js tambahan
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/jquery.validate.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/jquery_validation/localization/messages_id.min.js', 'header');
		$this->layout->set_include('js', 'themes/general/bundle/select2/select2.min.js', 'header');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/jquery-ui.js');
		$this->layout->set_include('js', 'themes/back/bundle/datepicker/datepicker-id.js');
		$this->layout->set_include('css', 'themes/back/bundle/datepicker/datepicker.css');
		$this->layout->set_include('js', 'themes/back/bundle/select2/select2.min.js');
		$this->layout->set_include('css', 'themes/back/bundle/select2/select2.min.css');

		//breadcrumb/untuk navigasi
		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Beranda', site_url());
		$this->breadcrumb->add_crumb('Klaim Obat ABHP', site_url('apotik'));
		$this->breadcrumb->add_crumb('Edit Klaim Obat ABHP');

		//judul besar
		$data['primary_title'] = 'Edit';
		$data['sub_primary_title'] = 'Edit data klaim obat & ABHP';

		//ambil data dari model dengan berdasarkan parameter diatas
		$data['list'] = $this->m_apotik->get_by($params);
		$data['dokter'] = $this->m_apotik->get_dokter_edit($params);
		$data['unit'] = $this->m_apotik->get_unit_edit($params);

		//menggunakan theme back
		$this->layout->theme('back','apotik/edit', $data);
	}

	function update() {
		//rule type pada function ini: Create
		$this->rule->type('U');
		$user = $this->ion_auth->user()->row();
		$id=$this->input->post('id_klaim_obat');
			$dataUpdate = array(
				'tgl_transaksi' => iddate2dbdate($this->input->post('tgl_transaksi')),
				'kd_dokter'=>$this->input->post('dokter'),
				'kd_unit'=>$this->input->post('unit'),
				'dmk'=>$this->input->post('dmk'),
			);

			//simpan melalui model
			//$this->m_apotik->save($dataInsert);

			if(!$this->m_apotik->update($id,$dataUpdate)){
				$this->session->set_flashdata('gagal', 'Kode klaim <b>'.$this->input->post('kd_klaim').'</b> dari peserta <b>'.$this->input->post('nama').'</b> gagal dirubah.');
				redirect('apotik','refresh');
			}else{
				$this->session->set_flashdata('sukses', 'Kode klaim <b>'.$this->input->post('kd_klaim').'</b> dari peserta <b>'.$this->input->post('nama').'</b> berhasil dirubah.');
				redirect('apotik/cetak/'.$id,'refresh');
			}

			redirect('apotik/cetak/'.$id);
		
	}

	/*
	function delete($id_klaim_obat = '') {

		//rule type pada function ini: Delete
		$this->rule->type('D');

		if(!$this->m_apotik->deleteObat($id_klaim_obat)){
			
		}else{
			if (!$this->m_apotik->delete($id_klaim_obat)) {
				$this->session->set_flashdata('gagal', 'Data gagal dihapus.');
				redirect('apotik','refresh');
			}else{
				$this->session->set_flashdata('sukses', 'Data berhasil dihapus.');
				redirect('apotik','refresh');
			}
		}
	} */

	

	function excel(){

		$this->rule->type('R');
		//load our new PHPExcel library
		$this->load->library('excel');
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle('Data Klaim Obat ABHP');
		//set cell A1 content with some text
		$this->excel->getActiveSheet()->setCellValue('A1', 'Data Klaim Obat ABHP');
		$this->excel->getActiveSheet()->setCellValue('A2', 'No.');
		$this->excel->getActiveSheet()->setCellValue('B2', 'Tanggal');
		$this->excel->getActiveSheet()->setCellValue('C2', 'Nama');
		$this->excel->getActiveSheet()->setCellValue('D2', 'Kd Klaim');
		$this->excel->getActiveSheet()->setCellValue('E2', 'No. DMK');
		$this->excel->getActiveSheet()->setCellValue('F2', 'Unit');
		$this->excel->getActiveSheet()->setCellValue('G2', 'Dokter');
		$this->excel->getActiveSheet()->setCellValue('H2', 'Petugas');
		$this->excel->getActiveSheet()->setCellValue('I2', 'Nama Obat/ABHP');
		$this->excel->getActiveSheet()->setCellValue('J2', 'Tarif Perda');
		$this->excel->getActiveSheet()->setCellValue('K2', 'Jumlah');
		$this->excel->getActiveSheet()->setCellValue('L2', 'Tarif Diminta');
		$this->excel->getActiveSheet()->setCellValue('M2', 'Sub Total');
		$this->excel->getActiveSheet()->setCellValue('N2', 'Total');
		$this->excel->getActiveSheet()->setCellValue('O2', 'Jenis Obat');
		$this->excel->getActiveSheet()->setCellValue('P2', 'Total');
		$this->excel->getActiveSheet()->setCellValue('Q2', 'G');
		$this->excel->getActiveSheet()->setCellValue('R2', '%G');
		$this->excel->getActiveSheet()->setCellValue('S2', 'P');
		$this->excel->getActiveSheet()->setCellValue('T2', 'Jasa Resep');
		$this->excel->getActiveSheet()->setCellValue('U2', 'Jasa Portir');
		$this->excel->getActiveSheet()->setCellValue('V2', 'BIaya Pemakaian O2');
		//change the font size
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(11);
		//make the font become bold
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//merge cell A1 until D1
		$this->excel->getActiveSheet()->mergeCells('A1:V1');
		//set aligment to center for that merged cell (A1 to D1)
		$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		//set style
	  $TitleStyle = array(
	    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
        )
	    ),
	    'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '00994C') // hijau tua
      ),
      'font'  => array(
	      'bold'  => false,
	      'color' => array('rgb' => 'FFFFFF'),
	      'size'  => 15,
	      'name'  => 'Verdana'
  		)
  	);

	  $HeaderStyle = array(
	    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
        )
	    ),
	    'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => '32CD32') // hijau
      ),
	  'font'  => array(
	      'bold'  => false,
	      'size'  => 11,
	      'name'  => 'Verdana'
  		)
  	);

  	$ContentStyle = array(
	    'borders' => array(
        'allborders' => array(
            'style' => PHPExcel_Style_Border::BORDER_THIN
        )
	    ),
	    'fill' => array(
            'type' => PHPExcel_Style_Fill::FILL_SOLID,
            'color' => array('rgb' => 'FFFFFF') // kuning
      	),
      	 'aligment' =>array(
            'vertical'=> PHPExcel_Style_Alignment::VERTICAL_CENTER
        ),
       'font'  => array(
	      'bold'  => false,
	      'size'  => 11,
	      'name'  => 'Verdana'
  		)
  	);
  	
		$this->excel->getActiveSheet()->getStyle('A1:V1')->applyFromArray($TitleStyle);
		$this->excel->getActiveSheet()->getStyle('A2:V2')->applyFromArray($HeaderStyle);
		//isi
		$apotik = $this->m_apotik->get_export_apotik();
		$grand_total = 0;
	  	$row = 3;
	  	$no = 1;
		foreach ($apotik as $value) {
			$this->excel->getActiveSheet()->setCellValue('A'.$row, $no);
			$this->excel->getActiveSheet()->setCellValue('B'.$row, tgl($value['tgl_transaksi']));
			$this->excel->getActiveSheet()->setCellValue('C'.$row, $value['nama']);
			$this->excel->getActiveSheet()->setCellValue('D'.$row, $value['klaim']." ");
			$this->excel->getActiveSheet()->setCellValue('E'.$row, $value['DMK']." ");
			$this->excel->getActiveSheet()->setCellValue('F'.$row, $value['unit']." ");
			$this->excel->getActiveSheet()->setCellValue('G'.$row, $value['dokter']." ");
			$this->excel->getActiveSheet()->setCellValue('H'.$row, $value['first_name']." ".$value['last_name']);
			
			

			$obat = $this->m_apotik->get_export_obat($keyword=$value['id_klaim_obat']);
			$obat_p = $this->m_apotik->get_export_jenisobat($params=array($keyword=$value['id_klaim_obat'],$jenis='P'));
			$obat_g = $this->m_apotik->get_export_jenisobat($params=array($keyword=$value['id_klaim_obat'],$jenis='G'));
			$portir = $this->m_apotik->get_export_jasa($params=array($keyword=$value['id_klaim_obat'],$kode='P'));
			if($portir==NULL){
				$tarif_portir=0;
			}else{
				$tarif_portir=$portir['tarif_diminta']*$portir['frekuensi_diminta'];
			}
			$resep = $this->m_apotik->get_export_jasa($params=array($keyword=$value['id_klaim_obat'],$kode='R'));
			if($resep==NULL){
				$tarif_resep=0;
			}else{
				$tarif_resep=$resep['tarif_diminta']*$resep['frekuensi_diminta'];
			}
			$oksigen = $this->m_apotik->get_export_jasa($params=array($keyword=$value['id_klaim_obat'],$kode='O2'));
			if($oksigen==NULL){
				$tarif_oksigen=0;
			}else{
				$tarif_oksigen=$oksigen['tarif_diminta']*$oksigen['frekuensi_diminta'];
			}

			$jumlah=count($obat);
			$total=0;
			$total_obat=0;
			$rows=$row;
			$mulai=$row;
			foreach ($obat as $list) {
				$subtotal=$list['tarif_diminta']*$list['frekuensi_diminta'];
				$this->excel->getActiveSheet()->setCellValue('I'.$rows, $list['obat']." ".$list['merk']);
				$this->excel->getActiveSheet()->setCellValue('J'.$rows, formatRupiah($list['tarif_perda']));
				$this->excel->getActiveSheet()->setCellValue('K'.$rows, $list['frekuensi_diminta']);
				$this->excel->getActiveSheet()->setCellValue('L'.$rows, formatRupiah($list['tarif_diminta']));
				$this->excel->getActiveSheet()->setCellValue('M'.$rows, formatRupiah($subtotal));
				$this->excel->getActiveSheet()->setCellValue('O'.$rows, $list['kd_jenis_obat']);
				
				$total=$total+$subtotal;

				//style
				$this->excel->getActiveSheet()->getStyle('A' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('B' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('C' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('D' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('E' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('F' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('G' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('H' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('I' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('J' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('K' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('L' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('M' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('N' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('O' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('P' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('Q' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('R' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('S' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('T' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('U' . $rows)->applyFromArray($ContentStyle);
				$this->excel->getActiveSheet()->getStyle('V' . $rows)->applyFromArray($ContentStyle);

				//vertical
				$this->excel->getActiveSheet()->getStyle('A' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('B' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('C' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('D' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('E' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('F' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('G' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('H' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('N' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('O' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('P' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('Q' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('R' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('S' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('T' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('U' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				$this->excel->getActiveSheet()->getStyle('V' . $rows)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
				//horizontal
				$this->excel->getActiveSheet()->getStyle('K' . $rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$this->excel->getActiveSheet()->getStyle('O' . $rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$this->excel->getActiveSheet()->getStyle('P' . $rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$this->excel->getActiveSheet()->getStyle('Q' . $rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$this->excel->getActiveSheet()->getStyle('R' . $rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				$this->excel->getActiveSheet()->getStyle('S' . $rows)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

				
			$rows++;	
			$total_obat++;	
			}
			$persen=($obat_g['tot']/$total_obat)*100;
			$selesai=$rows-1;
			//merge cells
			$this->excel->setActiveSheetIndex(0)->mergeCells('A'.$mulai.':A'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('B'.$mulai.':B'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('C'.$mulai.':C'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('D'.$mulai.':D'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('E'.$mulai.':E'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('F'.$mulai.':F'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('G'.$mulai.':G'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('H'.$mulai.':H'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('N'.$mulai.':N'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('P'.$mulai.':P'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('Q'.$mulai.':Q'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('R'.$mulai.':R'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('S'.$mulai.':S'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('T'.$mulai.':T'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('U'.$mulai.':U'.$selesai.'');
			$this->excel->setActiveSheetIndex(0)->mergeCells('V'.$mulai.':V'.$selesai.'');

			$this->excel->getActiveSheet()->setCellValue('N'.$row, formatRupiah($total));
			$this->excel->getActiveSheet()->setCellValue('P'.$row, $total_obat);
			$this->excel->getActiveSheet()->setCellValue('Q'.$row, $obat_g['tot']);
			$this->excel->getActiveSheet()->setCellValue('R'.$row, (int)$persen."%");
			$this->excel->getActiveSheet()->setCellValue('S'.$row, $obat_p['tot']);
			$this->excel->getActiveSheet()->setCellValue('T'.$row, formatRupiah($tarif_resep));
			$this->excel->getActiveSheet()->setCellValue('U'.$row, formatRupiah($tarif_portir));
			$this->excel->getActiveSheet()->setCellValue('V'.$row, formatRupiah($tarif_oksigen));
			//style
			
			$no++;
			$row=$rows;
		}
		
		/*
		$cols = 3;
		$num = 1;
		foreach($list_data as $row){
			$col = $cols++;
			$this->excel->getActiveSheet()->setCellValue('A' . $col, $num++);
			$this->excel->getActiveSheet()->setCellValue('B' . $col, $row['agama']);
		}

		//warna
		$cols2 = 3;
		foreach($list_data as $row){
			$col2 = $cols2++;
			$this->excel->getActiveSheet()->getStyle('A' . $col2)->applyFromArray($ContentStyle);
			$this->excel->getActiveSheet()->getStyle('B' . $col2)->applyFromArray($ContentStyle);
		}
		*/
		$this->excel->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("C")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("D")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("E")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("F")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("G")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("H")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("I")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("J")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("K")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("L")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("M")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("N")->setAutoSize(true);
		$this->excel->getActiveSheet()->getColumnDimension("O")->setAutoSize(true);

		
		$filename='Master Data Klaim Obat ABHP.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
		            
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');
	}

	function pdf(){
		// rule type
		$this->rule->type('R');
		//ambil data
		$data['results'] = $this->m_apotik->get_export();
		// pdf library
		$this->load->library('pdf');
		ob_start();		
		$this->load->view('back/layouts/apotik/pdf', $data);
    $content = ob_get_clean();
  	try{
  		// L => Landscape, P => Potrait, array(215,330) => ukuran kertas, array(10, 10, 5, 5) => margin
      $html2pdf = new HTML2PDF('P', array(215,330), 'en', true, 'UTF-8', array(10, 10, 5, 5));
      $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
      $html2pdf->Output("data_agama.pdf");
  	}
  	catch(HTML2PDF_exception $e) {
      echo $e;
      exit;
  	}
	}

	function get_klaim() {
		$kd_peserta  	= $this->input->post('kd_peserta');
		//echo $kd_peserta;
		$data 				= $this->m_apotik->get_peserta($kd_peserta);
		$jumlah 			= count($data);
		if($jumlah > 0){
			$arr_data = array(
				'stts'		=> "ada",
				'nama'		=> $data['nama'],
				'nopeserta'	=> $data['peserta'],
				'alamat'	=> $data['alamat'],
				'ttl'	=> $data['tempat_lahir'].", ".tgl_saja($data['tgl_lahir']),
				);
		}else{
			$arr_data = array(
				'stts'		=> "tdk",
				'nama'		=> "Tidak ditemukan, masukan Kd. Peserta yang benar",
				'nopeserta'	=> "Tidak ditemukan, masukan Kd. Peserta yang benar",
				'alamat'	=> "Tidak ditemukan, masukan Kd. Peserta yang benar",
				'ttl'	=> "Tidak ditemukan, masukan Kd. Peserta yang benar",
			);
		}
		echo json_encode($arr_data);
	}

	function get_apotik() {
		//$keyword 	= $this->input->post('kd_peserta');
		$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		$data['apotik'] 	= $this->m_apotik->get_apotik($keyword);
		$data['jumlah'] 	= count($data['apotik']);
		$this->load->view('back/layouts/apotik/ajax/apotik',$data);
		
	}

	function get_apotik_kode() {
		//$keyword 	= $this->input->post('kd_peserta');
		$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		$data['apotik'] 	= $this->m_apotik->get_apotik($keyword);
		$data['jumlah'] 	= count($data['apotik']);
		$this->load->view('back/layouts/apotik/ajax/apotik_kode',$data);
		
	}

	function get_obat_by() {
		$keyword 	= $this->input->post('obat');
		//$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		//echo $keyword;
		$params=array($keyword);
		$data 				= $this->m_apotik->get_obat_by($keyword);
		$jumlah 			= count($data);
		if ($jumlah>0) {
			$arr_data= array(
				'kd_obat'=> $data['kd_obat'],
				'obat'	=> $data['obat'],
				'harga'	=> $data['harga'],
				'jumlah'	=> 1,
				);
		}else{
			$arr_data= array(
				'kd_layanan'=> "",
				'layanan'	=> "",
				'harga'	=> "",
				'jumlah'	=> "",
				);

		}

		echo json_encode($arr_data);
	}

	function get_apotik_klaim() {
		//$keyword 	= $this->input->post('kd_apotik');
		$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		//echo $keyword;
		$data['apotik_klaim'] 	= $this->m_apotik->get_apotik_klaim($keyword);
		$data['jumlah'] 		= count($data['apotik_klaim']);
		$this->load->view('back/layouts/apotik/ajax/apotik_klaim',$data);
		
	}

	function simpan_obat()
	{
		
		$perda=$this->input->post('tarif_perda');
		$diminta=$this->input->post('tarif_diminta');
		if ($perda>$diminta) {
			$feedback=$diminta;
		}elseif ($diminta>$perda) {
			$feedback=$perda;
		}elseif($perda=$diminta){
			$feedback=$perda;
		}
		$data = array(
            'kd_obat' => $this->input->post('kd_obat'),
            'id_klaim_obat' => $this->input->post('klaim_obat'),
            'tarif_perda' => $this->input->post('tarif_perda'),
            'tarif_diminta' => $this->input->post('tarif_diminta'),
            'frekuensi_diminta' => $this->input->post('jumlah'),
            'frekuensi_feedback1' => $this->input->post('jumlah'),
            'tarif_feedback1' => $feedback,
                //'brgShow' => 1,   
        );

		$kode=$this->input->post('kd_obat');
		$klaim_obat=$this->input->post('klaim_obat');
		$params=array($kode,$klaim_obat);
		$cek=$this->m_apotik->cek($params)->num_rows();
		if($cek>0){
			$this->m_apotik->save_obat();
		}else{
        	$this->m_apotik->save_obat($data);
		}


	}

	

	function edit_obat() {
		$keyword 	= $this->input->post('obat');
		//$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		//echo $keyword;
		//echo '<select>';
		$data['obat_klaim'] 	= $this->m_apotik->get_obat_klaim_by($keyword);
		$data['jumlah'] 		= count($data['obat_klaim']);
		$this->load->view('back/layouts/apotik/ajax/apotik_edit',$data);
		
	}

	function update_obat()
	{
		
		$perda=$this->input->post('tarif_perda');
		$diminta=$this->input->post('tarif_diminta');
		$id=$this->input->post('id');
		if ($perda>$diminta) {
			$feedback=$diminta;
		}elseif ($diminta>$perda) {
			$feedback=$perda;
		}elseif($perda=$diminta){
			$feedback=$perda;
		}
		$dataUpdate = array(
            'tarif_perda' => $this->input->post('tarif_perda'),
            'tarif_diminta' => $this->input->post('tarif_diminta'),
            'frekuensi_diminta' => $this->input->post('jumlah'),
            'frekuensi_feedback1' => $this->input->post('jumlah'),
            'tarif_feedback1' => $feedback,
                //'brgShow' => 1,   
        );

        $this->m_apotik->update_obat($id,$dataUpdate);


	}

	function delete_obat(){
		$id=$this->input->post('obat');
		$keyword=$this->input->post('klaim');
			
		if($this->m_apotik->delete_obat($id)){
			$data['apotik_klaim'] 	= $this->m_apotik->get_apotik_klaim($keyword);
			$data['jumlah'] 		= count($data['apotik_klaim']);
			$this->load->view('back/layouts/apotik/ajax/apotik_klaim',$data);
			
		}else{
			echo "Gagal";
			
		}
	}

	//dokter dan unit
	function get_dokter() {
		//$keyword 	= $this->input->post('kd_peserta');
		$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		$data['dokter'] 	= $this->m_apotik->get_dokter($keyword);
		$data['jumlah'] 	= count($data['dokter']);
		$this->load->view('back/layouts/apotik/ajax/dokter',$data);
		
	}
	function get_unit() {
		//$keyword 	= $this->input->post('kd_peserta');
		$keyword 	= $this->uri->segment(3);
		//$keyword 	= $_GET('kd_peserta');
		$data['unit'] 	= $this->m_apotik->get_unit($keyword);
		$data['jumlah'] 	= count($data['unit']);
		$this->load->view('back/layouts/apotik/ajax/unit',$data);
		
	}

}
